package world.xuewei.service.impl;

import org.springframework.stereotype.Service;
import world.xuewei.dao.CommodityReviewDao;
import world.xuewei.model.entity.CommodityReview;
import world.xuewei.service.CommodityReviewService;

import javax.annotation.Resource;
import java.util.List;

@Service
public class CommodityReviewServiceImpl implements CommodityReviewService {

    @Resource
    private CommodityReviewDao commodityReviewDao;

    @Override
    public boolean addReview(CommodityReview review) {
        if (review == null || review.getCommodityId() == null ||
                review.getUserId() == null || review.getRating() == null) {
            return false;
        }
        return commodityReviewDao.insertReview(review) > 0;
    }

    @Override
    public boolean deleteReview(Long id, Long userId) {
        if (id == null || userId == null) {
            return false;
        }
        // 查询评论是否存在且属于当前用户
        CommodityReview review = commodityReviewDao.queryReviewById(id);
        if (review == null || !review.getUserId().equals(userId)) {
            return false;
        }
        return commodityReviewDao.deleteReviewById(id) > 0;
    }

    @Override
    public List<CommodityReview> getReviewsByCommodityId(Long commodityId) {
        if (commodityId == null) {
            return null;
        }
        return commodityReviewDao.queryReviewsByCommodityId(commodityId);
    }
}